<template>
    <div class="add-role">
      <header class="add-header">
        <div class="left">
          <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
          <span>|</span>
          <span>添加角色</span>
        </div>

        <div class="right">
          黑马程序员
          <svg-icon icon-class="back"/>
        </div>
      </header>
      <main class="add-main">
        <div class="step-container">
          <el-steps direction="vertical" :active="1">
            <el-step title="角色信息" />
            <el-step title="权限信息" />
            <el-step title="检查并完成" />
          </el-steps>
        </div>
        <div class="form-container" v-show="activeStep===1">
          <div class="title">角色信息</div>
          <div class="form">
        <el-form class="form-box" :model="roleForm" :rules="roleRules" ref="roleForm">
            <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="roleForm.roleName" />
            </el-form-item>
            <el-form-item label="角色描述">
            <el-input v-model="roleForm.remark" />
            </el-form-item>
        </el-form>
        </div>
        </div>
        <div class="form-container" v-show="activeStep===2">
          <div class="title">权限配置</div>
          <div class="form">
            <template>
              <div class="tree-wrapper">
                <div v-for="item in treeList" :key="item.id" class="tree-item">
                  <div class="tree-title">{{ item.title }}</div>
                  <el-tree
                    ref="tree"
                    :data="item.children"
                    show-checkbox
                    default-expand-all
                    node-key="id"
                    highlight-current
                    :props="{ label: 'title' }"
                  />
                </div>
              </div>
            </template>
          </div>
        </div>
        <div v-show="activeStep === 3" class="form-container">
  <div class="title">检查并完成</div>
  <div class="form">
    <div class="info">
      <div class="form-item">角色名称：{{ roleForm.roleName }}</div>
      <div class="form-item">角色描述：{{ roleForm.remark }}</div>
      <div class="form-item">角色权限：</div>
      <div class="tree-wrapper">
        <div v-for="item in disabledTreeList" :key="item.id" class="tree-item">
          <div class="tree-title">{{ item.title }}</div>
          <el-tree
            ref="disabledTreeList"
            :data="item.children"
            show-checkbox
            default-expand-all
            node-key="id"
            :highlight-current="false"
            :props="{ label: 'title'}"
          />
        </div>
      </div>
    </div>
  </div>
</div>
      </main>
      <footer class="add-footer">
        <div class="btn-container">
          <el-button v-if="activeStep>1" @click="decreseStep">上一步</el-button>
          <el-button v-if="activeStep<3" type="primary" @click="nextStep">下一步</el-button>
          <el-button v-if="activeStep === 3" type="primary" @click="confirmAdd">确认添加</el-button>
        </div>
      </footer>
    </div>
  </template>

<script>
import { getTreeData, createRoleUserAPI } from '@/api/Role'
// 处理禁用函数
export function addDisabled (treeList) {
  treeList.forEach(item => {
    item.disabled = true
    // 递归处理
    if (item.children && item.children.length > 0) {
      addDisabled(item.children)
    }
  })
}
export default {
  data () {
    return {
      disabledTreeList: [],
      treeList: [], // 权限树形列表
      activeStep: 1, // 步骤条当前步骤
      roleForm: {
        roleName: '',
        remark: ''
      },
      roleRules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    // 在初始化时候就加载好
    this.getTreeList()
  },
  methods: {
    // 上一步
    decreseStep () {
      if (this.activeStep === 1) return
      this.activeStep--
    },
    nextStep () {
      if (this.activeStep === 3) return
      if (this.activeStep === 1) {
        // 当前是角色信息状态
        // 当前是角色信息状态，进行表单校验
        this.$refs.roleForm.validate(valid => {
          if (valid) {
            // 校验通过才能进入下一个状态
            this.activeStep++
          }
        })
      } else if (this.activeStep === 2) {
        // 当前是权限信息状态
        this.roleForm.perms = []
        this.$refs.tree.forEach(tree => {
          this.roleForm.perms.push(tree.getCheckedKeys())
        })
        // 如果长度为零 没有选中任何东西
        if (this.roleForm.perms.flat().length === 0) {
          this.$message({
            type: 'error',
            message: '请至少选择一个权限点'
          })
        } else {
          // 如果长度不为零，进入到检查并完成
          this.activeStep++
          // 回填已选择数据
          this.$refs.disabledTreeList.forEach((tree, index) => {
            tree.setCheckedKeys(this.roleForm.perms[index])
          })
        }
      }
    },
    async getTreeList () {
      const res = await getTreeData()
      this.treeList = res.data
      this.disabledTreeList = JSON.parse(JSON.stringify(res.data))
      // 处理禁用
      addDisabled(this.disabledTreeList)
    },
    async confirmAdd () {
      await createRoleUserAPI(
        this.roleForm
      )
      this.$message({
        type: 'success',
        message: '添加角色成功'
      })
      this.$router.back()
    }
  }
}
</script>
<style scoped lang="scss">
.add-role {
  background-color: #f4f6f8;
  min-height: 100vh;
  display: flex;
  flex-direction: column;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;
    flex-shrink: 0;

    .left {
      span {
        margin-right: 4px;
        &:last-child {
          color: #303133;
        }
      }
      .arrow {
        cursor: pointer;
        color: #409eff;
        &:hover {
          opacity: 0.8;
        }
      }
    }
  }

  .add-main {
    flex: 1;
    display: flex;
    padding: 20px 150px;
    background: #f4f6f8;
    overflow: hidden;

    .step-container {
      width: 220px;
      padding: 20px;
      background: #fff;
      border-radius: 4px;
      margin-right: 20px;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);

      ::v-deep .el-steps {
        height: 100%;
        .el-step__title {
          font-size: 14px;
        }
      }
    }

    .form-container {
      flex: 1;
      background: #fff;
      border-radius: 4px;
      overflow: hidden;
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);

      .title {
        height: 60px;
        line-height: 60px;
        padding: 0 20px;
        border-bottom: 1px solid #ebeef5;
        font-weight: 500;
      }

      .form {
        padding: 30px;
        min-height: 500px;
        box-sizing: border-box;
      }
    }
  }

  .add-footer {
    height: 88px;
    background: #fff;
    border-top: 1px solid #ebeef5;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    .btn-container {
      display: flex;
      gap: 20px;

      .el-button {
        min-width: 120px;
      }
    }
  }

  @media (max-width: 1200px) {
    .add-main {
      padding: 20px 50px;
    }
  }

  @media (max-width: 768px) {
    .add-main {
      padding: 15px;
      flex-direction: column;

      .step-container {
        width: 100%;
        margin-right: 0;
        margin-bottom: 15px;
      }
    }

    .form-container .form {
      padding: 20px;
    }
  }
}
</style>
